#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<assert.h>

using namespace std;

namespace zy
{
	class string
	{
	public:
		/*string()
			:_str(new char('\0'))
			,_size(0)
			,_capacity(0)
		{}

		string(const char* s)
			:_size(strlen(s))
		{
			_capacity = _size;
			_str = new char[_capacity + 1];
			strcpy(_str, s);
		}*/
		string(const char* s = "")
			:_size(strlen(s))
		{
			_capacity = _size;
			_str = new char[_capacity + 1];
			strcpy(_str, s);
		}

		/*string(const string& s)
		{
			_str = new char[s._capacity + 1];
			_size = s._size;
			_capacity = s._capacity;

			strcpy(_str, s._str);
		}*/
		string(const string& s)
		{
			string tmp(s.c_str());
			swap(tmp);
		}


		/*string& operator=(const string& s)
		{
			delete[] _str;
			_str = new char[s._capacity + 1];
			_size = s._size;
			_capacity = s._capacity;

			strcpy(_str, s._str);
			return *this;
		}*/
		/*string& operator=(const string& s)
		{
			string tmp(s._str);
			swap(tmp);
			if (tmp._str)
				delete[] tmp._str;
			return *this;
		}*/
		string& operator=(string s)
		{
			swap(s);
			return *this;
		}

		~string()
		{
			if (_str)
			{
				delete _str;
				_str = nullptr;
			}
			_size = _capacity = 0;
		}

		typedef char* iterator;
		typedef const char* const_iterator;

		iterator begin()
		{
			return _str;
		}
		iterator end()
		{
			return _str + _size;
		}
		const_iterator begin() const
		{
			return _str;
		}
		const_iterator end() const
		{
			return _str + _size;
		}

		int size()
		{
			return _size;
		}
		int length()
		{
			return _size;
		}
		int capacity()
		{
			return _capacity;
		}
		const char* c_str() const
		{
			return _str;
		}

		char& operator[](size_t pos)
		{
			return _str[pos];
		}
		char operator[](size_t pos) const
		{
			return _str[pos];
		}

		void clear()
		{
			_size = 0;
			_str[_size] = '\0';
		}

		bool empty()
		{
			return _size == 0;
		}

		void reserve(size_t n = 0)
		{
			if (n > _size)
			{
				char* tmp = new char[n+1];
				strcpy(tmp, _str);
				delete _str;
				_str = tmp;

				_capacity = n;
			}
		}

		void resize(size_t n, char c = '\0')
		{
			if (n < _size)
			{
				_str[n] = '\0';
				_size = n;
			}
			else 
			{
				reserve(n);
				for (int i = _size; i < n; i++)
				{
					_str[i] = c;
				}
				_size = n;
				_str[_size] = '\0';
			}
		}

		string& insert(size_t pos, const char* s)
		{
			int len = strlen(s);
			reserve(_size + len);

			int i = _size;
			while (i >= (int)pos)
			{
				_str[i + len] = _str[i];
				i--;
			}

			for (int i = 0; i < len; i++)
			{
				_str[pos + i] = s[i];
			}
			_size += len;

			return *this;
		}

		string& insert(size_t pos, const char c)
		{
			if (_size == _capacity)
				_capacity == 0 ? reserve(4) : reserve(2 * _capacity);

			int end = _size;
			while (end >= (int)pos)
			{
				_str[end + 1] = _str[end];
				end--;
			}
			_str[pos] = c;
			_size++;

			return *this;
		}

		string& append(const char* s)
		{
			insert(_size, s);
			return *this;
		}

		string& push_back(const char c)
		{
			insert(_size, c);
			return *this;
		}

		string& operator+=(const char* s)
		{
			append(s);
			return *this;
		}
		string& operator+=(const char c)
		{
			push_back(c);
			return *this;
		}

		string& erase(size_t pos = 0, size_t len = npos)
		{
			if (len == npos || len >= _size - pos)
			{
				_str[pos] = '\0';
				_size = pos;
			}
			else
			{
				for (int i = pos+len; i < _size; i++)
				{
					_str[i - len] = _str[i];
				}
				_size -= len;
				_str[_size] = '\0';
			}
			return *this;
		}

		void swap(string& s)
		{
			std::swap(_str, s._str);
			std::swap(_size, s._size);
			std::swap(_capacity, s._capacity);
		}

		size_t find(const char* s, size_t pos = 0) const
		{
			assert(pos < _size);
			const char* p = strstr(_str + pos, s);
			if (p)
			{
				return p - _str;
			}
			return npos;
		}
		size_t find(const char c, size_t pos = 0) const
		{
			assert(pos < _size);
			for (int i = pos; i < _size; i++)
			{
				if (_str[i] == c)
					return i;
			}
			return npos;
		}

		string substr(size_t pos = 0, size_t len = npos) const
		{
			assert(pos < _size);
			string sub;
			if (len == npos || len >= _size - pos)
			{
				for (int i = pos; i <= _size; i++)
				{
					sub.push_back(_str[i]);
				}
			}
			else
			{
				int i = pos;
				for (int j = 0; j < len; j++)
				{
					sub.push_back(_str[i++]);
				}
				sub.push_back('\0');
			}
			return sub;
		}

		bool operator==(const string& s)
		{
			if (strcmp(_str, s._str) == 0)
				return 1;
			return 0;
		}
		bool operator!=(const string& s)
		{
			return !(*this == s);
		}
		bool operator>(const string& s)
		{
			if (strcmp(_str, s._str) == 1)
				return 1;
			return 0;
		}
		bool operator>=(const string& s)
		{
			return *this > s || *this == s;
		}
		bool operator<(const string& s)
		{
			return !(*this >= s);
		}
		bool operator<=(const string& s)
		{
			return !(*this > s);
		}



	private:
		char* _str = nullptr;
		size_t _size;
		size_t _capacity;
		static int npos;
	};

	int string::npos = -1;

	void swap(string& s1, string& s2)
	{
		s1.swap(s2);
	}

	ostream& operator<<(ostream& out, const string& s)
	{
		for (auto e : s)
		{
			out << e;
		}
		return out;
	}
	istream& operator>>(istream& in, string& s)
	{
		s.clear();
		char buf[128] = { '\0' };
		char c = in.get();
		int i = 0;
		while (c != ' ' && c != '\n')
		{
			buf[i++] = c;
			if (i == 127)
			{
				buf[127] = '\0';
				s += buf;
				i = 0;
			}
			c = in.get();
		}
		if (i > 0)
		{
			buf[i] = '\0';
			s += buf;
		}


		/*char c = in.get();
		while (c != ' ' && c != '\n')
		{
			s += c;
			c = in.get();
		}*/


		/*char c = getchar();
		while (c != '\n')
		{
			s += c;
			c = getchar();
		}*/
		return in;
	}

	istream& getline(istream& in, string& s)
	{
		s.clear();
		char buf[128];
		char c = in.get();
		int i = 0;
		while (c != '\n')
		{
			buf[i++] = c;
			if (i == 127)
			{
				buf[127] = '\0';
				s += buf;
				i = 0;
			}
			c = in.get();
		}
		if (i > 0)
		{
			buf[i] = '\0';
			s += buf;
		}


		/*char c = in.get();
		while (c != ' ' && c != '\n')
		{
			s += c;
			c = in.get();
		}*/


		/*char c = getchar();
		while (c != EOF)
		{
			s += c;
			c = getchar();
		}*/
		return in;
	}
}